<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/7/25
 * Time: 14:07
 */
namespace app\api\controller;
use app\models\Models;

class Scan extends Base {
    /**
     * 我的二维码
     * @return \think\response\Json
     */
    public function QRCode(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt']);
                $user = new Models('user');
                $info = $user->_find(['id'=>$params['uid']],'id,nickname,portrait');
                $info['portrait'] = empty($info['portrait']) ? '' : self::arrayMapPortrait($info['portrait']);
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($info));
        }
    }

    /**
     * 扫码进店
     * @return \think\response\Json
     */
    public function scanCode(){
        if(request()->isPost()){
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt','business_id']);
                $scan = new Models('scan');
                $info = $scan->_find(['uid'=>$params['uid'],'bid'=>$params['business_id'],'status'=>Models::SCAN_YES]);
                if($info){
                    throw new \LogicException('您已扫码过了',1040);
                }
                // 将扫过其他店铺的状态取消
                $bool = $scan->_save(['status'=>Models::SCAN_NOT],['uid'=>$params['uid'],'status'=>Models::SCAN_YES]);
                // 添加新店的扫码状态
                $bool1 = $scan->_add(['uid'=>$params['uid'],'bid'=>$params['business_id'],'create_time'=>time()]);
                if(false === $bool || !$bool1){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }
}